package edu.guet.server.service;

import java.util.List;
import java.util.List;
import java.util.Map;

import edu.guet.server.mapper.ResourceMapper;
import edu.guet.server.model.dto.UnitDTO;
import edu.guet.server.model.dto.UnitResourceDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.guet.server.mapper.UnitMapper;
import edu.guet.server.model.Unit;
import edu.guet.server.utils.ResultAPI;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UnitService {
    @Autowired
    UnitMapper unitMapper;
    @Autowired
    ResourceMapper resourceMapper;

    public ResultAPI setUnit(Unit unit) {
        unitMapper.setUnit(unit);
        return ResultAPI.success(unit,"设置成功");
    }

    public Unit findUnit(Unit unit) {
        Unit result = unitMapper.findUnit(unit);
        return result;
    }
    
    //参数主要来源于用户旧的单元 (old_unit) 和 想要改写的单元数据 (date)
    public ResultAPI resetUnit(Unit date){

        Unit old_unit = findUnit(date);

        int old_unit_semester_id = old_unit.getSemester_id();
        String old_unit_title = old_unit.getUnit_title();
        int old_unit_module = old_unit.getUnit_module();
        int old_unit_resource_cnt = old_unit.getResource_cnt();

        int new_unit_semester_id = date.getSemester_id();
        String new_unit_title = date.getUnit_title();
        int new_unit_module = date.getUnit_module();
        int new_unit_resource_cnt = date.getResource_cnt();

        Unit new_unit = new Unit();
        new_unit.setUnit_id(old_unit.getUnit_id());

        if (old_unit_semester_id == new_unit_semester_id){
            new_unit.setSemester_id(old_unit_semester_id);
        } else {
            new_unit.setSemester_id(new_unit_semester_id);
        }

        if (old_unit_title.trim().equals(new_unit_title.trim())){
            new_unit.setUnit_title(old_unit_title);
        } else {
            new_unit.setUnit_title(new_unit_title);
        }

        if (old_unit_module == new_unit_module){
            new_unit.setUnit_module(old_unit_module);
        } else {
            new_unit.setUnit_module(new_unit_module);
        }

        if (old_unit_resource_cnt == new_unit_resource_cnt){
            new_unit.setResource_cnt(old_unit_resource_cnt);
        } else {
            new_unit.setResource_cnt(new_unit_resource_cnt);
        }

        unitMapper.resetUnit(new_unit);
        return ResultAPI.success(new_unit, "修改成功");
    }

    /**
     * 按照semester_id查找对应章节数量
     * @param semester_id
     * @return
     */
    public Integer countUnit(Integer semester_id){
        return unitMapper.countUnit(semester_id);
    }

    /**
     * 加单元资源（视频、ppt等
     * @param unitId
     * @param resourceId
     */
    @Transactional
    public void addUnitResource(Integer unitId, Integer resourceId) {
        unitMapper.addUnitResource(unitId,resourceId);
        unitMapper.updateUnitCount(unitId, 1);
    }

    /**
     * 根据单元id和资源类型找单元资源列表
     * @param unitId
     * @param resourceType
     */
    public List<UnitResourceDTO> showUnitResouList(Integer unitId, String resourceType) {
       return unitMapper.showUnitResouList(unitId,resourceType);
    }

    public List<UnitDTO> findUnits(Integer semesterId) {
        List<UnitDTO> units = unitMapper.findUnits(semesterId);
        return units;
    }

    public String findUnitName(Integer unitId) {
        String unitName = unitMapper.findUnitName(unitId);
        return unitName;
    }

    @Transactional
    public void deleteUnitResource(UnitResourceDTO unitResourceDTO) {
        unitMapper.deleteUnitResource(unitResourceDTO);
        unitMapper.updateUnitCount(unitResourceDTO.getUnit_id(),-1);
    }

    public void deleteUnit(Integer unitId) {
        unitMapper.deleteUnit(unitId);
    }

    //根据课程id查找单元
    public ResultAPI findUnitBySemesterId(int semester_id){
        List<Unit> units = unitMapper.findUnitBySemesterId(semester_id);
        return ResultAPI.success(units, "查询成功");
    }

    public ResultAPI deleteUnitById(int unit_id){
        boolean flag = unitMapper.deleteUnitById(unit_id);
        if (flag){
            return ResultAPI.success("删除成功");
        } else {
            return ResultAPI.error("删除失败");
        }
    }

    public ResultAPI updateUnitTitle(UnitDTO unitDTO) {
        boolean flag = unitMapper.updateUnitTitle(unitDTO);
        if (flag){
            return ResultAPI.success("更新成功");
        } else {
            return ResultAPI.error("更新失败");
        }
    }
}
